<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataGoods: async () => Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
    SelectImageRadio: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectImageRadio()),
    SelectImageCheckbox: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectImageCheckbox()
      ),
  },
  data() {
    return {
      appWindow: window,
      form: {
        // 分销等级
        level: 0,
        // 分销自购
        self_buy: 0,
        // // 是否需要审核
        // need_check: true,
        // 成为分销商条件
        become: 1,
        // 申请页面顶部图片
        background_img: "",
        // 显示申请协议
        open_protocol: true,
        // 成为分销商条件-消费次数
        become_order_count: null,
        // 消费条件
        become_order_status: 1,
        // 成为分销商条件-消费金额
        become_money_count: null,
        // 指定商品
        become_goods: [],
        // 完善资料 0需要 1不需要
        become_register: 0,
        // 资料内容
        become_register_form: ["姓名"],
        // 成为下线条件
        // become_child: 1,
        // 商品默认参与分销
        goods_join_commission: 0,
        // 商品样式
        become_good_style: 1,
        // 分销商特权
        commission_privilege_switch: true,
        // 分销商特权
        commission_privilege: "无",
        become_check: 1,
        
      },

      load: true,

      levelList: [],

      goods_ids: [],
    };
  },
  methods: {
    // 验证 
    validate() {
      return new Promise((resolve, reject) => {
          this.$refs["editForm"].validate((valid) => {
              valid ? resolve(true) : resolve(false)
          });
      });
    },
    add() {
      if(this.form.become_register_form.length == 5) {
        return;
      }
      this.form.become_register_form.push("");
    },
    del(item, index) {
      if(this.form.become_register_form.length == 1) {
        return;
      }else {
        this.$delete(this.form.become_register_form, index);
      }
    },
    async getData() {
      let res2 = await this.$vFN.Http.post({
        url: "plugin/distribution/backend/levels/index?pageNum=1&pageSize=20",
      }, window);
      this.levelList = [];
      res2.data.list.forEach(item => [
        this.levelList.push({
          id: item.id,
          name: item.name,
          goods_ids: item.goods_ids,
        })
      ])
      let res = await this.$vFN.Http.post({
        url: "plugin/distribution/backend/settings/base/get",
      }, window);
      this.form = res.data;
      this.form.become_check = 1;

      if(this.form.become_goods && this.levelList) {
        this.form.become_goods.forEach(item1 => {
          this.levelList.forEach(item2 => {
            if(item2.id == item1.id) {
              item2.goods_ids = item1.goods;
            }
          })
        })
      }
      
      this.load = false;
    },

    async sum() {
      if (!await this.validate()) {
        return
      }
      this.load = true;
      let _f;
      this.form.become_goods = [];
      this.levelList.forEach((item, index) => {
        _f = {
          id: item.id,
          goods: item.goods_ids
        }
        this.form.become_goods.push(_f);
      })
      let res = await this.$vFN.Http.post({
        url: "plugin/distribution/backend/settings/base/set",
        data: this.form,
      }, window);
      this.form = res.data;
      if(res.code == 200) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.getData();
      }
    },
    // 初始化
    async init() {
      this.getData();
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>